package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.SysRoleMapper;
import com.atguigu.spzx.manager.service.SysRoleService;
import com.atguigu.spzx.model.dto.system.SysRoleDto;
import com.atguigu.spzx.model.entity.system.SysRole;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    SysRoleMapper sysRoleMapper;

    @Override
    public PageInfo<SysRole> findByPage(Integer pageNum, Integer pageSize, SysRoleDto sysRoleDto) {
        //1.开启分页功能
        PageHelper.startPage(pageNum,pageSize); //原理：将数据通过ThreadLocal,与当前线程进行绑定。

        //2.数据查询
        List<SysRole> list =  sysRoleMapper.findByPage(sysRoleDto);  //  返回对象其实就是线程绑定Page对象    Page extends ArrayList

        //3.封装数据返回结果
        PageInfo<SysRole> pageInfo = new PageInfo<>(list); //默认导航页数量8
        return pageInfo;
    }

    @Override
    public void save(SysRole sysRole) {
        sysRoleMapper.insert(sysRole);
    }

    @Override
    public SysRole getById(Long id) {
        return sysRoleMapper.getById(id);
    }

    @Override
    public void update(SysRole sysRole) {
        sysRoleMapper.update(sysRole);
    }

    @Override
    public void deleteById(Long id) {
        sysRoleMapper.deleteById(id);
    }

    @Override
    public Map<String, List> findAllRoles(Long userId) {
        List<SysRole> allRolesList = sysRoleMapper.findAll();

        List<Long> sysUserRoles = sysRoleMapper.findRoleIdsByUserId(userId);

        //存放两个部分数据
        Map<String, List> map = new HashMap<>();

        //后端给前端返回多个部分数据时，可以通过Map或Vo封装数据。
        map.put("allRolesList",allRolesList); //查询所有角色
        map.put("sysUserRoles",sysUserRoles); //查询用户所拥有的角色id集合
        return map;
    }
}
